<template>
  <div class="detail-enterprise">
    <header class="add-header">
      <el-page-header content="查看企业" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">租赁记录</div>
        <div class="table-container">
          <el-table
            :data="form.rent"
            style="width: 100%"
            border
          >
            <el-table-column
              type="index"
              label="序号"
              width="50"
            />
            <el-table-column
              prop="name"
              label="租赁楼宇"
              width="180"
            />
            <el-table-column
              label="租赁起止时间"
              width="280"
            >
              <template #default="{row}">
                {{ row.startTime }} - {{ row.endTime }}
              </template>
            </el-table-column>
            <el-table-column
              label="租赁合同(点击预览)"
            >
              <template #default="{row}">
                <el-button type="text">
                  <a :href="row.contractUrl" target="_blank">
                    {{ row.contractName }}
                  </a>
                </el-button>
              </template>
            </el-table-column>
            <el-table-column
              prop="createTime"
              label="录入时间"
            />
            <el-table-column
              label="操作"
            >
              <template #default="{row}">
                <el-button type="text" @click="downloadContract(row.contractUrl)">合同下载</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </main>
  </div>
</template>

<script>
import { getEnterpriseDetailAPI } from '@/api/enterprise'

export default {
  name: 'EnterpriseDetail',
  data() {
    return {
      form: {},
      previewURL: 'https://view.officeapps.live.com/op/view.aspx?src=' // 预览地址
    }
  },
  mounted() {
    this.getDetail()
  },
  methods: {
    async getDetail() {
      try {
        const res = await getEnterpriseDetailAPI(this.$route.query.id)
        if (res.data && res.data.rent) {
          res.data.rent.forEach(obj => {
            let url = obj.contractUrl
            console.log('Original Contract URL:', url) // 调试日志

            // 确保 URL 包含协议
            if (!url.startsWith('http://') && !url.startsWith('https://')) {
              url = 'http://' + url // 或者 'https://'
            }

            // 获取文件扩展名
            const extName = url.slice(url.lastIndexOf('.')).toLowerCase()

            // 处理不同类型的文件
            if (extName === '.doc' || extName === '.docx') {
              // 使用在线预览工具
              obj.contractUrl = this.previewURL + encodeURIComponent(url)
            } else if (extName === '.pdf') {
              // 直接打开 PDF
              obj.contractUrl = url
            } else {
              // 其他文件类型，直接打开
              obj.contractUrl = url
            }

            console.log('Processed Contract URL:', obj.contractUrl) // 调试日志
          })
        }
        this.form = res.data
      } catch (error) {
        console.error('获取企业详情失败:', error)
      }
    },
    downloadContract(url) {
      console.log('Download URL:', url) // 调试日志
      window.open(url, '_blank')
    }
  }
}
</script>

<style scoped lang="scss">
.detail-enterprise {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }
      .arrow{
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;
      margin-bottom: 20px;
      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }
    }
    .table-container{
      padding:20px;
    }
    .preview{
      img{
        width: 100px;
      }
    }
  }
}
</style>
